package com.ovhoo.vdm.vdm;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import com.ovhoo.vdm.vdm.VDMLoader.ApiException;

public class VDMLoginManager {
	
	private static VDMLoginManager uniqueInstance = null;
	
	//POST http://api.betacie.com/account/login/<login>/<pass_md5>
	private final String loginurl = "http://api.betacie.com/account/login/";
	
	private String login = null;; //Login
	private String password = null;; // Encrypted login MD5
	private String token = null;
	
	private NetFileLoader netFileLoader = null;
	
	private VDMLoginManager(){
		this.netFileLoader = new NetFileLoader();
	}
	
	public static VDMLoginManager getInstance(){
		if (VDMLoginManager.uniqueInstance == null){
			VDMLoginManager.uniqueInstance = new VDMLoginManager();
		}
		
		return VDMLoginManager.uniqueInstance;
	}
	
	public void login(String login, String password, String lng){
		MessageDigest _digest;
		try {
			_digest = MessageDigest.getInstance("MD5");
			_digest.update(password.getBytes());
			BigInteger _hash = new BigInteger(1, _digest.digest());
			
			this.login = login;
			this.password = _hash.toString(16);;
			
			String _response = netFileLoader.getUrlContent(this.loginurl + this.login + "/" +  this.password+"?language="+ lng + "&key=" + VDMPropertyManager.developprKey);
			
			if (_response != null){
				if (_response.length() > 0){
					this.token = _response;
				}
			}
			
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String getToken(){
		return this.password;
	}
	


}
